﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReportingServices.DeploymentCenter.Core;
using ReportingServices.DeploymentCenter.Core.ReportingServicesServiceReference;

namespace ReportingServices.DeploymentCenter.Console
{
    class Program
    {
        #region Properties

        private static bool DeploymentConfigurationIsValid
        {
            get
            {
                if (String.IsNullOrWhiteSpace(Properties.Settings.Default.ReportServer))
                {
                    System.Console.WriteLine("Please enter a value for the report server in the configuration file.");
                    return false;
                }

                if (String.IsNullOrWhiteSpace(Properties.Settings.Default.Domain))
                {
                    System.Console.WriteLine("Please enter a value for the domain in the configuration file.");
                    return false;
                }

                if (String.IsNullOrWhiteSpace(Properties.Settings.Default.UserName))
                {
                    System.Console.WriteLine("Please enter a value for the user name the configuration file.");
                    return false;
                }

                if (String.IsNullOrWhiteSpace(Properties.Settings.Default.Password))
                {
                    System.Console.WriteLine("Please enter a value for the password the configuration file.");
                    return false;
                }

                if (String.IsNullOrWhiteSpace(Properties.Settings.Default.DeploymentFolder))
                {
                    System.Console.WriteLine("Please enter a value for the deployment folder in the configuration file.");
                    return false;
                }

                if (Properties.Settings.Default.DeploymentFiles == null || Properties.Settings.Default.DeploymentFiles.Count == 0)
                {
                    System.Console.WriteLine("Please enter at least one file to deploy in the configuration file.");
                    return false;
                }

                return true;
            }
        }

        public static ServiceConfiguration ReportingServicesServiceConfiguration
        {
            get
            {
                return new ServiceConfiguration(Properties.Settings.Default.ReportServer,
                                                Properties.Settings.Default.Domain,
                                                Properties.Settings.Default.UserName,
                                                Properties.Settings.Default.Password);
            }
        }

        #endregion

        #region Methods

        static void Main(string[] args)
        {
            System.Console.WriteLine("Beginning deployment...");

            if (DeploymentConfigurationIsValid)
            {
                using (DeploymentManager deploymentManager = new DeploymentManager(ReportingServicesServiceConfiguration))
                {
                    string deploymentFolder = Properties.Settings.Default.DeploymentFolder;
                    string[] pathParts = deploymentFolder.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);              

                    CatalogItem[] serverFolders = deploymentManager.GetReportServerFolders("/", true);
                    List<string> serverPaths = serverFolders.Select(i => i.Path).ToList();

                    if (!serverPaths.Contains(deploymentFolder))
                        deploymentManager.CreateFolder(deploymentFolder);

                    deploymentManager.OnItemDeployed += DeploymentManager_OnItemDeployed;

                    string[] filePaths = new string[Properties.Settings.Default.DeploymentFiles.Count];
                    Properties.Settings.Default.DeploymentFiles.CopyTo(filePaths, 0);

                    deploymentManager.DeployFiles(deploymentFolder, filePaths, Properties.Settings.Default.OverwriteExistingItems);

                    deploymentManager.OnItemDeployed -= DeploymentManager_OnItemDeployed;
                }

                System.Console.WriteLine("Deployment complete.");
            }
            else
            {
                System.Console.WriteLine("Deployment canceled due to invalid configuration.");
            }

            System.Console.ReadLine();
        }

        static void DeploymentManager_OnItemDeployed(object sender, ItemDeployedEventArgs e)
        {
            if (e == null || e.DeploymentResult == null)
                return;

            if (e.DeploymentResult.Warnings != null)
            {
                foreach (Warning warning in e.DeploymentResult.Warnings)
                {
                    System.Console.WriteLine(warning.Message);
                }
            }

            if (e.DeploymentResult.ItemInformation != null)
            {
                System.Console.WriteLine(String.Format("{0} successfully uploaded.",e.DeploymentResult.ItemInformation.Name));
            }
        }

        #endregion
    }
}
